﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.RegInfo.Entities;

namespace BusinessLayer.RegInfo
{
    public class TransactionLogBL
    {
        private readonly DbHelper _db;

        public TransactionLogBL()
        {
            _db = new DbHelper(new DbConfig().DbRegInfoConnectionString);         
        }

        public void Insert(int accountId, int subscriptionId, int typePay, string couponCode, string merchTxnRef, out decimal amount)
        {
            var oCommand = new SqlCommand("sp_TransactionLog_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@AccountId", accountId));
            oCommand.Parameters.Add(new SqlParameter("@SubscriptionId", subscriptionId));
            oCommand.Parameters.Add(new SqlParameter("@TypePay", typePay));
            oCommand.Parameters.Add(new SqlParameter("@CouponCode", couponCode));
            oCommand.Parameters.Add(new SqlParameter("@MerchTxnRef", merchTxnRef));
            var output = new SqlParameter("@Amount", SqlDbType.Decimal) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            amount = (decimal)output.Value;
        }

        public void Approve(string merchTxnRef, string status, string message)
        {
            var oCommand = new SqlCommand("sp_TransactionLog_Approve") { CommandType = CommandType.StoredProcedure };            
            oCommand.Parameters.Add(new SqlParameter("@MerchTxnRef", merchTxnRef));
            oCommand.Parameters.Add(new SqlParameter("@Status", status));
            oCommand.Parameters.Add(new SqlParameter("@Message", message));
            _db.ExecuteNonQuery(oCommand);
        }
    }
}
